<template>
    <div>
        <div class="container">
            <el-page-header @back="goBack" content="出库单详情"></el-page-header>

            <!-- 简要信息 -->
            <el-descriptions class="e-m-t-45" :column="2" title="简要信息" border :labelStyle="{width:'120px'}">
                <el-descriptions-item label="编号">{{form.number}}</el-descriptions-item>
                <el-descriptions-item label="状态">
                    <span class="color-success" v-if="form.state == '已出库'">{{form.state}}</span>
                    <span class="color-success" v-if="form.state == '已完结'">{{form.state}}</span>
                    <span class="color-danger" v-if="form.state == '已取消'">{{form.state}}</span>
                </el-descriptions-item>
                <el-descriptions-item label="出库站点">
                    <div>{{form.store.name}}</div>
                    <!-- <div :class="item.state == '已取消'?'del-line':''" v-for="(item,index) in form.storeoutorder" :key="index">{{item.store.name}}<span v-if="item.state == '已取消'">(已撤销)</span></div> -->
                </el-descriptions-item>
                <el-descriptions-item label="出库到">
                    <div v-if="form.packinorder">{{form.packinorder.packname}}<span class="tips">(类型：打包站)</span></div>
                    <div v-if="form.manufacturer">{{form.manufacturer.name}}<!--span class="tips">(类型：厂商)</span--></div>
                </el-descriptions-item>
                <el-descriptions-item label="负责人" v-if="form.supercargo">{{ form.supercargo.name }}</el-descriptions-item>
                <el-descriptions-item label="完结时间">{{form.endtime}}</el-descriptions-item>
                <el-descriptions-item label="所属加盟商" v-if="form.franchise">{{form.franchise.name}}</el-descriptions-item>
                <!-- <el-descriptions-item label="出库总数量">{{form.weight}}KG/{{ form.quantity }}件</el-descriptions-item>
                <el-descriptions-item label="出库总价值">{{form.amount}}元</el-descriptions-item> -->
                <!-- <el-descriptions-item label="差额值" v-if="form.state == '已完结'">{{form.weightdifference}}KG/{{ form.quantitydifference }}件</el-descriptions-item>
                <el-descriptions-item label="对比差额" v-if="form.state == '已完结'">
                    <el-tag type="danger" v-if="form.isexception">大差额</el-tag>
                    <el-tag type="success" v-else>正常</el-tag>
                </el-descriptions-item>
                <el-descriptions-item label="审查" v-if="form.state == '已完结'">
                    <el-tag v-if="form.examinestate == '已审核'" type="success">已审核</el-tag>
                    <el-button v-else type="primary" @click="examine">未审核</el-button>
                </el-descriptions-item>
                <el-descriptions-item label="接收打包站"></el-descriptions-item>
                <el-descriptions-item label="审核人" v-if="form.state == '已完结'">{{ form.examinename || '--' }}</el-descriptions-item> -->
                <el-descriptions-item label="出库备注" span="2" v-if="form.remarks">{{form.remarks}}</el-descriptions-item>
            </el-descriptions>
            <!-- /简要信息 -->
            
            <!-- 出库清单汇总 -->
            <h4>出库清单汇总</h4>
            <div class="flex">
                <div class="e-flex-1">
                    <el-table :data="dataarr" :span-method="objectSpanMethod2" show-summary :summary-method="getSummaries" stripe border style="width: 100%">
                        <el-table-column
                            prop="large"
                            label="类型"
                            width="200">
                        </el-table-column>
                        <el-table-column
                            prop="typenames"
                            label="可回收物"
                            >
                        </el-table-column>
                        <el-table-column
                            prop="price"
                            label="出库估值单价(元/KG)"
                            width="200">
                            <template slot-scope="scope">
                                <span>{{ scope.row.price }}{{ scope.row.isweighing?'元/KG':'元/件' }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column
                            prop="weight"
                            label="出库数量"
                            width="200">
                            <template slot-scope="scope">
                                <span>{{ scope.row.weight }}{{ scope.row.isweighing?'KG':'件' }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column
                            prop="amount"
                            label="出库估值"
                            width="200">
                            <template slot-scope="scope">
                                <span>{{ scope.row.amount }}元</span>
                            </template>
                        </el-table-column>
                        <el-table-column
                            prop="typeremarks"
                            label="备注"
                            width="200">
                            <template slot-scope="scope">
                                <span>{{ scope.row.typeremarks }}</span>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="total">
                        <span>共有{{ dataarr.length }}种品类</span>
                        <div class="e-flex-1"></div>
                        <template v-if="form.ismanage">
                            <span class="e-m-r-10">出库总成本：<span class="bold">￥{{ form.cost }}</span></span>
                            <span class="e-m-r-10">预估销售金额：<span class="bold">￥{{ form.amount }}</span></span>
                            <span>估计利润：<span class="bold">￥{{ form.profit }}</span></span>
                        </template>    
                    </div>
                    <h4 v-if="form.proves.length > 0">出库上传图片</h4>
                    <div class="images-list e-m-t-30">
                        <div class="item" v-for="(item, index) in form.proves" :key="index">
                            <el-image style="width: 150px; height: 150px; border-radius: 6px;" :src="fileURL + item"
                            :preview-src-list=[[fileURL+item]] fit="cover"></el-image>
                        </div>
                    </div>
                </div>
            </div>
            <template v-if="form.state == '已出库'">
                <!-- /出库清单汇总 -->
                <h4>核实出库</h4>
                <el-button type="primary" icon="el-icon-plus" plain @click="dialogVisible = true">添加记录</el-button>

                <el-table
                    ref="multipleTable"
                    tooltip-effect="dark"
                    :data="stockList"
                    :span-method="stockListSpanMethod"
                    stripe
                    border
                    style="width: 100%; margin-top: 20px"
                >
                    <el-table-column prop="typenames" label="类型" width="200">
                        <template slot-scope="scope">
                            <h4 class="text-center">{{ scope.row.typenames }}</h4>
                        </template>
                    </el-table-column>
                    <el-table-column prop="name" label="可回收物" width="120">
                        <template slot-scope="scope">
                            <span class="text-center">{{ scope.row.name }}({{ scope.row.isweighing?'KG':'件' }})</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="毛重">
                        <template slot-scope="scope">
                            <el-input type="number" v-model="scope.row.grossweight" placeholder="请填写毛重">
                                <div slot="suffix" style="line-height: 40px;">{{ scope.row.isweighing?'KG':'件' }}</div>
                            </el-input>
                        </template>
                    </el-table-column>
                    <el-table-column label="皮重">
                        <template slot-scope="scope">
                            <el-input type="number" v-model="scope.row.tareweight" placeholder="请填写皮重">
                                <div slot="suffix" style="line-height: 40px;">{{ scope.row.isweighing?'KG':'件' }}</div>
                            </el-input>
                        </template>
                    </el-table-column>
                    <el-table-column label="扣重">
                        <template slot-scope="scope">
                            <el-input type="number" v-model="scope.row.deductionweight" placeholder="请填写扣重">
                                <div slot="suffix" style="line-height: 40px;">{{ scope.row.isweighing?'KG':'件' }}</div>
                            </el-input>
                        </template>
                    </el-table-column>
                    <el-table-column label="扣点">
                        <template slot-scope="scope">
                            <el-input type="number" v-model="scope.row.deductionratio" placeholder="请填写扣点">
                                <div slot="suffix" style="line-height: 40px;">%</div>
                            </el-input>
                        </template>
                    </el-table-column>
                    <el-table-column label="出库价">
                        <template slot-scope="scope">
                            <el-input type="number" v-model="scope.row.price" placeholder="请填写单价">
                                <div slot="suffix" style="line-height: 40px;">元/{{ scope.row.isweighing?'KG':'件' }}</div>
                            </el-input>
                        </template>
                    </el-table-column>
                    <el-table-column label="总金额">
                        <template slot-scope="scope">
                            <el-input type="number" v-model="scope.row.amount" placeholder="请填写金额">
                                <div slot="suffix" style="line-height: 40px;">元</div>
                            </el-input>
                        </template>
                    </el-table-column>
                    <el-table-column label="备注(选填)">
                        <template slot-scope="scope">
                            <el-input type="text" v-model="scope.row.remarks" placeholder="请填写备注"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="100">
                        <template slot-scope="scope">
                            <el-button type="danger" size="mini" @click="delRow(scope.$index, scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                
                <h4>上传图片</h4>
                <div class="images-list e-m-t-30">
                    <div class="item" v-for="(item, index) in confirmEnd.proves" :key="index">
                        <el-image style="width: 150px; height: 150px; border-radius: 6px;" :src="fileURL + item"
                        :preview-src-list=[[fileURL+item]] fit="cover"></el-image>
                        <span class="el-icon-error err" @click.stop="delFile(index)"></span>
                    </div>
                    <div class="item">
                        <cropper-upload @getUpload="handleFilesSuccess" class="avatar-uploader">
                            <div class="avatar-uploader-icon">
                                <div><i class="el-icon-plus"></i></div>
                            </div>
                        </cropper-upload>
                    </div>
                </div>
            </template>
            <template v-if="form.state == '已完结'">
                <h4>核实出库</h4>
                <div class="flex">
                    <div class="e-flex-1">
                        <el-table
                            ref="multipleTable"
                            tooltip-effect="dark"
                            :data="stockList"
                            :span-method="stockListSpanMethod"
                            show-summary
                            :summary-method="getSummaries"
                            stripe
                            border
                            style="width: 100%; margin-top: 20px"
                        >
                            <el-table-column prop="typenames" label="类型" width="200">
                                <template slot-scope="scope">
                                    <h4 class="text-center">{{ scope.row.typenames }}</h4>
                                </template>
                            </el-table-column>
                            <el-table-column prop="name" label="可回收物" width="200">
                                <template slot-scope="scope">
                                    <span class="text-center">{{ scope.row.name }}({{ scope.row.isweighing?'KG':'件' }})</span>
                                </template>
                            </el-table-column>
                            <el-table-column label="单价">
                                <template slot-scope="scope">
                                    <span>{{ scope.row.price }}元/{{ scope.row.isweighing?'KG':'件' }}</span>
                                </template>
                            </el-table-column>
                            <el-table-column label="毛重">
                                <template slot-scope="scope">
                                    <span>{{ scope.row.grossweight }}{{ scope.row.isweighing?'KG':'件' }}</span>
                                </template>
                            </el-table-column>
                            <el-table-column label="皮重">
                                <template slot-scope="scope">
                                    <span>{{ scope.row.tareweight }}{{ scope.row.isweighing?'KG':'件' }}</span>
                                </template>
                            </el-table-column>
                            <el-table-column label="扣重">
                                <template slot-scope="scope">
                                    <span>{{ scope.row.deductionweight }}{{ scope.row.isweighing?'KG':'件' }}</span>
                                </template>
                            </el-table-column>
                            <el-table-column label="扣点">
                                <template slot-scope="scope">
                                    <span>{{scope.row.deductionratio}}%</span>
                                </template>
                            </el-table-column>
                            <el-table-column label="金额(元)">
                                <template slot-scope="scope">
                                    <span>{{ scope.row.amount }}元</span>
                                </template>
                            </el-table-column>
                            <el-table-column label="备注">
                                <template slot-scope="scope">
                                    <span>{{ scope.row.remarks }}</span>
                                </template>
                            </el-table-column>
                        </el-table>
                        <div class="total">
                            <span>共有{{ stockList.length }}种品类</span>
                            <div class="e-flex-1"></div>
                            <template v-if="form.ismanage">
                                <span class="e-m-r-10">售卖总金额：<span class="bold">￥{{ form.sellamount }}</span></span>
                                <span>售卖利润：<span class="bold">￥{{ form.sellprofit }}</span></span>
                            </template>
                        </div>
                    </div>
                </div>
                <h4 v-if="form.sellproves.length > 0">核实出库上传图片</h4>
                <div class="images-list e-m-t-30">
                    <div class="item" v-for="(item, index) in form.sellproves" :key="index">
                        <el-image style="width: 150px; height: 150px; border-radius: 6px;" :src="fileURL + item"
                        :preview-src-list=[[fileURL+item]] fit="cover"></el-image>
                    </div>
                </div>
                
            </template>
            <!-- 已完结时查看出库回执明细 -->
            <!-- <template v-if="form.packdetails.length > 0">
                <h4>出库回执明细</h4>
                <div class="flex">
                    <div class="e-flex-1">
                        <el-table border :span-method="objmoney" :data="moneyarr" show-summary :summary-method="getSummaries" stripe style="width: 100%">
                            <el-table-column prop="large" label="类型" fixed width="200">
                            </el-table-column>
                            <el-table-column prop="typenames" label="小类" fixed>
                            </el-table-column>
                            <el-table-column
                                prop="weight"
                                label="重量/数量"
                                width="200">
                                <template slot-scope="scope">
                                    <span>{{ scope.row.netweight}}{{ scope.row.isweighing?'KG':'件' }}</span>
                                </template>
                            </el-table-column>
                            <el-table-column
                                prop="price"
                                label="单价(元/KG)"
                                width="200">
                            </el-table-column>
                            <el-table-column
                                prop="amount"
                                label="金额(元)"
                                width="200">
                            </el-table-column>
                            <el-table-column
                                prop="typeremarks"
                                label="备注"
                                >
                            </el-table-column>
                        </el-table>
                    </div>
                </div>
                <h4>上传的回执</h4>
                <div class="images-list">
                    <div class="item" v-for="(item, index) in form.endproves" :key="index">
                        <el-image
                            style="width: 150px; height: 150px; border-radius: 6px;"
                            :src="fileURL + item"
                            :preview-src-list=[[fileURL+item]]
                            fit="cover"></el-image>
                    </div>
                </div>
            </template> -->
            <!-- /已完结时查看出库回执明细 -->
            <el-form label-width="0" class="e-m-t-45">
                <el-form-item>
                    <el-button @click="goBack">返回</el-button>
                    <el-button type="danger" v-if="form.iscancel" @click="dialogVisible2 = true">取消</el-button>
                    <el-button type="primary" @click="deliverStore" v-if="form.isend">结单</el-button>
                </el-form-item>
            </el-form>
        </div>

        <el-dialog
            title="添加回执单"
            :visible.sync="dialogVisible"
            width="800px"
            :append-to-body="true"
            >
            <el-form ref="addForm" :model="packDetails" :rules="rules" label-width="140px">
                <el-form-item label="选择可回收物">
                    <selectProduct v-model="packDetails.productid" @select="chooseProduct"></selectProduct>
                </el-form-item>
                <el-form-item :label="`${packDetails.isweighing?'毛重(KG)':'数量(件)'}`">
                    <el-input v-model="packDetails.grossweight" type="digit" placeholder="请输入数字" :border="true"></el-input>
                </el-form-item>
                <el-form-item :label="`${packDetails.isweighing?'皮重(KG)':''}`" v-if="packDetails.isweighing == true">
                    <el-input v-model="packDetails.tareweight" type="digit" placeholder="请输入数字" :border="true"></el-input>
                </el-form-item>
                <el-form-item :label="`${packDetails.isweighing?'扣重(KG)':'扣件(件)'}`">
                    <el-input v-model="packDetails.deductionweight" type="digit" placeholder="请输入数字" :border="true"></el-input>
                </el-form-item>
                <el-form-item :label="`扣点(%)`">
                    <el-input v-model="packDetails.deductionratio" type="digit" placeholder="请输入数字" :border="true"></el-input>
                </el-form-item>
                <el-form-item :label="`单价(元/${packDetails.isweighing?'KG':'件'})`">
                    <el-input v-model="packDetails.price" type="digit" placeholder="请输入数字" :border="true"></el-input>
                </el-form-item>
                <el-form-item label="总金额(元)">
                    <el-input v-model="packDetails.amount" type="digit" placeholder="请输入数字" :border="true"></el-input>
                </el-form-item>
                <el-form-item label="备注">
                    <el-input v-model="packDetails.remarks" type="textarea" placeholder="请输入备注" :border="true"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="confirmSelect(packDetails)">确定</el-button>
            </span>
        </el-dialog>

        <!-- 取消出库订单 -->
        <el-dialog
            title="取消出库订单"
            :visible.sync="dialogVisible2"
            width="500px"
            :append-to-body="true"
            >
            <el-form>
                <!-- <el-form-item>
                    <el-select v-model="chooseStores" placeholder="请选择站点" clearable>
                        <el-option
                            v-for="item in cancelStores"
                            :key="item.id"
                            :label="item.store.name"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item> -->
                <el-form-item>
                    <el-input type="textarea" :rows="5" placeholder="请输入取消订单的理由" v-model="cancalReason" maxlength="200"></el-input>
                </el-form-item>
            </el-form>
            <div class="e-p-t-10 e-p-b-10">
                <div class="tips">说明：</div>
                <div class="tips">选择站点对应取消那个站点的出库单；不选择站点提交则整个出库单取消。</div>
            </div>
            
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible2 = false">取消</el-button>
                <el-button type="primary" @click="cancalOrder">确定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
	export default {
		layout: 'admin',
		head() {
			return {
				title: '出库单详情' 
			}
		},
        props:{
            id: {
                type: Number,
                default: 0
            }
        },
		data() {
			return {
                moneyarr:[],
                dataarr:[],
                form:{
                    store:{
                        id: 0,
                        name: '',
                    },
                    proves:[],
                    packstation:{},
                    brother: {},
                    deliverdetails: [],
                    delivers:{
                        name: '',
                        details: [],
                        loadingproves: [],
                        deliverproves: [],
                    },
                    // loadingproves: [],
                    // deliverproves: [],
                    endproves: [],
                    packdetails: [],
                    amount:0,
                    storeoutorder: [],
                    // stores:[
                    //     {
                    //         amount: 0,
                    //         delivertime: '',
                    //         details: [
                    //             {
                    //                 details: [],
                    //                 icon: '',
                    //                 id: 0,
                    //                 isweighing: true,
                    //                 name: '',
                    //                 smallicon: '',
                    //                 sort: 0
                    //             }
                    //         ],
                    //         proves: [],
                    //         staff: {},
                    //         state: '',
                    //         store: {},
                    //         weight: 0,
                    //         details: []
                    //     }
                    // ],
                    packstation:{}
                },
                storeoutorder: [],
                activeNames: [],
                stockList: [], //录入出库回执列表

                chooseType: [],
                confirmEnd:{
                    orderId: 0,
                    packDetails: [
                        // {
                        //     "largeId": 0,
                        //     "typeId": 2147483647,
                        //     "remarks": "string",
                        //     "weight": 0,
                        //     "price": 0
                        // }
                    ],
                    proves: [],
                    endTime: ""
                },
                dialogVisible: false,
                tempArr: [],
                // 取消订单
                dialogVisible2: false,
                chooseStores: null,
                cancalReason: '',
                // cancelStores: [],
                // 回执信息
				packDetails: {
					typenames: '',
					productname: '',
					typeid: 0,
					productid: 0,
					remarks: "",
					price: null, //单价
					grossweight: null, //毛重
					tareweight: null, //皮重
					deductionweight: 0, //扣重
					deductionratio: 0, //扣点
					amount: null, //金额
                    isweighing: true, //是否按重量计算
				},
                rules:{

                }
            }
        },
        watch:{
            dialogVisible(val){
                if(val){
                    this.packDetails = {
                        typenames: '',
                        productname: '',
                        typeid: 0,
                        productid: 0,
                        remarks: "",
                        price: null, //单价
                        grossweight: null, //毛重
                        tareweight: null, //皮重
                        deductionweight: 0, //扣重
                        deductionratio: 0, //扣点
                        amount: null, //金额
                        isweighing: true, //是否按重量计算
                    }
                }
            }
        },
        mounted() {
            this.getDetails(this.id)
        },
        methods: {
            // 取消出库单
            cancalOrder(){
                // this.$prompt('请输入取消原因', '提示', {
                //     confirmButtonText: '确定',
                //     cancelButtonText: '取消',
                //     inputPattern: /^[\s\S]*.*[^\s][\s\S]*$/,
                //     inputErrorMessage: '请填写取消原因'
                // }).then(({ value }) => {
                //     this.$axios.post('/Store/OutOrder/Cancel', {
                //         id: this.id,
                //         reason: value
                //     }).then((res) => {
                //         this.$message({
                //             type: 'success',
                //             message: res.msg
                //         });
                //         this.getList()
                //     })
                // })
                console.log('chooseStores',this.chooseStores)
                // let param = {
                //     orderId: Number(this.id)
                // }
                // if(this.chooseStores > 0){
                //     param.storeId = this.chooseStores
                // }
                this.$axios.post(`/Store/OutOrder/Cancel`,{
                    id: this.id,
                    storeOutOrderId: this.chooseStores,
                    reason: this.cancalReason
                }).then((res) => {
                    this.$message({
                        type: 'success',
                        message: res.msg
                    });
                    this.getList()
                    this.dialogVisible2 = false
                })
            },
            //出库清单汇总合计
            getSummaries(param) {
                const { columns, data } = param;
                const sums = [];
                columns.forEach((column, index) => {
                    if (index === 0) {
                        sums[index] = '合计';
                        return;
                    }
                    const values = data.map(item => Number(item[column.property]));

                    // console.log(this.tableData)
                    if (!values.every(value => isNaN(value))) {
                        sums[index] = values.reduce((prev, curr) => {
                            const value = Number(curr);
                            if (!isNaN(value)) {
                                return prev + curr;
                            } else {
                                return prev;
                            }
                        }, 0);
                        sums[index] += '';
                    } else {
                        sums[index] = '';
                    }

                    if (column.label == '小类') {
                        sums[index] = '/'
                    }

                    if (column.label == '数量') {
                        let weight = 0
                        let num = 0
                        data.forEach((item)=>{
                            if(item.isweighing == true){
                                weight += item.weight
                            }else{
                                num += item.weight
                            }
                        })
                        sums[index] = weight + 'kg' + '  /  ' + num + '件'
                    }

                    if (column.label == '总价') {
                        let amount = 0
                        data.forEach((item)=>{
                            amount += item.amount
                        })
                        sums[index] = amount.toFixed(2) + '元'
                    }

                    if (column.label == '备注') {
                        sums[index] = '/'
                    }

                    if (column.label == '重量/数量') {
                        let weight = 0
                        let num = 0
                        data.forEach((item)=>{
                            if(item.isweighing == true){
                                weight += item.netweight
                            }else{
                                num += item.netweight
                            }
                        })
                        sums[index] = weight + 'kg' + '  /  ' + num + '件'
                    }

                    if (column.label == '出库数量') {
                        let weight = 0
                        let num = 0
                        data.forEach((item)=>{
                            if(item.isweighing == true){
                                weight += item.weight
                            }else{
                                num += item.weight
                            }
                        })
                        sums[index] = weight + 'kg' + '  /  ' + num + '件'
                    }

                    if (column.label == '单价(元/KG)') {
                        sums[index] = '/'
                    }

                    if (column.label == '金额(元)') {
                        let amount = 0
                        data.forEach((item)=>{
                            amount += item.amount
                        })
                        sums[index] = amount.toFixed(2) + '元'
                    }
                });

                return sums;
            },
            // examine(){
            //     //审查
            //     this.$confirm('是否审查此单？', '提示', {
            //         confirmButtonText: '确定',
            //         cancelButtonText: '取消',
            //         type: 'warning'
            //     }).then(() => {
            //         this.$axios.post('/Store/OutOrder/Examine?id='+this.id).then((res)=>{
            //             this.$message({
            //                 type: 'success',
            //                 message: '审查成功!'
            //             });
            //             this.getDetails(this.id)
            //         })
            //     })
            // },
            delRow(index,row){
                console.log('点击删除按钮',index,row)
                // this.$refs.chooseType.clearCheckedNodes()
                this.stockList.splice(index,1)
                this.chooseType.forEach((item,i)=>{
                    if(item[0] == row.largeId && item[1] == row.typeId){
                        this.chooseType.splice(i,1)
                    }
                })
                console.log(this.chooseType)
            },
            handleFilesSuccess(res, file) {
                this.confirmEnd.proves.push(res.path);
            },
            delFile(index) {
                this.confirmEnd.proves.splice(index, 1);
            },
            numberChange(e,max,id){
                // console.log(e.target.value)
            },
            stockListSpanMethod({ row, column, rowIndex, columnIndex }) {
                let mergeLength = 1;//需要进行横纵合并的列
                if (columnIndex < mergeLength) {
                    let finArray = [1, 1];
                    // 处理行数据
                    let cgname = "typeid"
                    if (rowIndex === 0 || row[cgname] !== this.stockList[rowIndex - 1][cgname]) {
                        let rowspan = 1;
                        //计算需要进行合并操作的行
                        for (let i = 0; i < this.stockList.length - 1; i++) {
                            // 只合并连续的
                            if (this.stockList[i][cgname] === row[cgname] && this.stockList[i + 1][cgname] === row[cgname]) {
                                rowspan++;
                            }
                        }
                        finArray[0] = rowspan;
                    } else {
                        finArray[0] = 0;
                    }
                    return finArray
                }
            },
            //选择品类
            confirmSelect(tempArr){
                console.log('确定添加回执单',tempArr)
                this.stockList.push(tempArr)
                this.dialogVisible = false
            },
            chooseProduct(obj){
                console.log('选择品类',obj)
                this.packDetails.typenames = obj.typenames
                this.packDetails.typeid = obj.typeid
                this.packDetails.name = obj.name
                this.packDetails.productid = obj.id
                this.packDetails.isweighing = obj.isweighing
            },
            //合并类型格子
            // objectSpanMethod({ row, column, rowIndex, columnIndex }) {
            //     // console.log(row,column, rowIndex, columnIndex,'objectSpanMethod')
            //     let mergeLength = 1;//需要进行横纵合并的列
            //     if (columnIndex < mergeLength) {
            //         let finArray = [1, 1];
            //         // 处理行数据
            //         let num = 0
            //         // let cgname = Object.keys(row)[7]//7是数字最后一项类型id
            //         let cgname = 'large'
            //         let arr = this.storeoutorder[rowIndex]
            //         if (rowIndex === 0 || row[cgname] !== arr[rowIndex - 1][cgname]) {
            //         let rowspan = 1;
            //         //   //计算需要进行合并操作的行
            //         for (let i = 0; i < arr.length - 1; i++) {
            //             // 只合并连续的
            //             if (arr[i][cgname] === row[cgname] && arr[i + 1][cgname] === row[cgname]) {
            //             rowspan++;
            //             }
            //         }
            //         finArray[0] = rowspan;
            //         } else {
            //         finArray[0] = 0;
            //         }
            //         return finArray
            //     }
            // },
            objectSpanMethod2({ row, column, rowIndex, columnIndex }) {
                // console.log(row,column, rowIndex, columnIndex,'objectSpanMethod')
                let mergeLength = 1;//需要进行横纵合并的列
                if (columnIndex < mergeLength) {
                    let finArray = [1, 1];
                    // 处理行数据
                    let num = 0
                    // let cgname = Object.keys(row)[7]//7是数字最后一项类型id
                    let cgname = 'large'
                    let arr = this.dataarr
                    if (rowIndex === 0 || row[cgname] !== arr[rowIndex - 1][cgname]) {
                    let rowspan = 1;
                    //   //计算需要进行合并操作的行
                    for (let i = 0; i < arr.length - 1; i++) {
                        // 只合并连续的
                        if (arr[i][cgname] === row[cgname] && arr[i + 1][cgname] === row[cgname]) {
                            rowspan++;
                        }
                    }
                        finArray[0] = rowspan;
                    } else {
                        finArray[0] = 0;
                    }
                    return finArray
                }
            },
            goBack() {
				this.$emit('goBack')
			},
            getDetails(id){ //获取详情
                this.dataarr = []
                this.moneyarr = []
                this.stockList = []
                this.$axios.get('/Store/OutOrder/Details?id='+id).then((res)=>{
                    console.log(res)
                    this.form = res
                    // 格式化出库总览
                    // 已出库时，汇总所有站点的出库数据
					// if(this.form.state == '已出库'){
                        // res.details.filter((c)=>c.state != '已取消').forEach((con)=>{
                            res.details.forEach((item)=>{
                                item.details.forEach((cell)=>{
                                    // 站点出库清单汇总
                                    this.dataarr.push({
                                        show: false,
                                        id: cell.productid,
                                        price: cell.sellprice,
                                        weight: cell.weight,
                                        amount: cell.sellamount,
                                        typeremarks: cell.remarks,
                                        allowedit: true,
                                        minprice: 0,
                                        highprice: 0,
                                        err: false,
                                        large: item.names,
                                        typenames: cell.productname,
                                        isweighing: true,
                                        typeid: item.id,
                                        pic: item.icon,
                                        isweighing:cell.isweighing,
                                        on: true,
                                        grossweight: cell.grossweight,
                                        tareweight: cell.tareweight,
                                        netweight: cell.netweight,
                                        deductionweight: cell.deductionweight,
                                        deductionratio: cell.deductionratio
                                    })
                                    if(this.form.state == '已出库'){
                                        // 预设核实出库列表
                                        this.stockList.push({
                                            typenames: item.names,
                                            name: cell.productname,
                                            typeid: item.id,
                                            productid: cell.productid,
                                            remarks: "",
                                            price: null, //单价
                                            grossweight: null, //毛重
                                            tareweight: null, //皮重
                                            deductionweight: 0, //扣重
                                            deductionratio: 0, //扣点
                                            amount: null, //金额
                                            isweighing: cell.isweighing, //是否按重量计算
                                        })
                                    }
                                })
                            })
                        // })
                        console.log('dataarr',this.dataarr)
                        // 格式化站点出库明细
                        // res.details.forEach((item, index) => {
                            // this.storeoutorder[index] = []
                            // res.details.forEach((cell,x)=>{
                            //     cell.details.forEach((i,y)=>{
                            //         let obj = {}
                            //         obj.amount = i.sellamount
                            //         obj.price = i.sellprice
                            //         obj.typenames = i.productname
                            //         obj.typepath = i.typepath
                            //         obj.weight = i.weight
                            //         obj.large = cell.names
                            //         obj.largeId = cell.id
                            //         obj.isweighing = i.isweighing
                            //         obj.typeremarks = i.remarks?i.remarks:'/'
                            //         obj.storeId = item.store.id
                            //         this.storeoutorder[index].push(obj)
                            //     })
                            // })
                        // })
                    // }
                    // 已完结时，显示回执单的汇总
					if(this.form.state == '已完结'){
                        res.selldetails.forEach((item)=>{
                            item.details.forEach((cell)=>{
                                this.stockList.push({
                                    show: false,
                                    id: cell.productid,
                                    price: cell.price,
                                    weight: cell.weight,
                                    amount: cell.amount,
                                    remarks: cell.remarks,
                                    allowedit: true,
                                    minprice: 0,
                                    highprice: 0,
                                    err: false,
                                    name: cell.productname,
                                    typenames: item.names,
                                    isweighing: true,
                                    typeid: item.id,
                                    pic: item.icon,
                                    isweighing:cell.isweighing,
                                    on: true,
                                    grossweight: cell.grossweight,
                                    tareweight: cell.tareweight,
                                    netweight: cell.netweight,
                                    deductionweight: cell.deductionweight,
                                    deductionratio: cell.deductionratio
                                })
                            })
                        })
                        // res.details.filter((c)=>c.state != '已取消').forEach((con)=>{
                            // res.details.forEach((item)=>{
                            //     item.details.forEach((cell)=>{
                            //         // 站点出库清单汇总
                            //         this.dataarr.push({
                            //             show: false,
                            //             id: cell.productid,
                            //             price: cell.sellprice,
                            //             weight: cell.weight,
                            //             amount: cell.sellamount,
                            //             typeremarks: cell.remarks,
                            //             allowedit: true,
                            //             minprice: 0,
                            //             highprice: 0,
                            //             err: false,
                            //             large: item.names,
                            //             typenames: cell.productname,
                            //             isweighing: true,
                            //             typeid: item.id,
                            //             pic: item.icon,
                            //             isweighing:cell.isweighing,
                            //             on: true,
                            //             grossweight: cell.grossweight,
                            //             tareweight: cell.tareweight,
                            //             netweight: cell.netweight,
                            //             deductionweight: cell.deductionweight,
                            //             deductionratio: cell.deductionratio
                            //         })
                            //         // 预设核实出库列表
                            //         // this.stockList.push({
                            //         //     typenames: item.names,
                            //         //     name: cell.productname,
                            //         //     typeid: item.id,
                            //         //     productid: cell.productid,
                            //         //     remarks: "",
                            //         //     price: null, //单价
                            //         //     grossweight: null, //毛重
                            //         //     tareweight: null, //皮重
                            //         //     deductionweight: 0, //扣重
                            //         //     deductionratio: 0, //扣点
                            //         //     amount: null, //金额
                            //         //     isweighing: cell.isweighing, //是否按重量计算
                            //         // })
                            //     })
                            // })
                        // })
                        console.log('dataarr',this.dataarr)
                        // 格式化站点出库明细
                        // res.details.forEach((item, index) => {
                        //     this.storeoutorder[index] = []
                        //     item.details.forEach((cell,x)=>{
                        //         cell.details.forEach((i,y)=>{
                        //             let obj = {}
                        //             obj.amount = i.sellamount
                        //             obj.price = i.sellprice
                        //             obj.typenames = i.productname
                        //             obj.typepath = i.typepath
                        //             obj.weight = i.weight
                        //             obj.large = cell.names
                        //             obj.largeId = cell.id
                        //             obj.isweighing = i.isweighing
                        //             obj.typeremarks = i.remarks?i.remarks:'/'
                        //             obj.storeId = item.store.id
                        //             this.storeoutorder[index].push(obj)
                        //         })
                        //     })
                        // })
                    }
                    
                    // this.cancelStores = res.details.filter((c)=>c.state != '已取消')
                    console.log('格式化后的form：',this.form)
                })
            },
            // 完结出库单
            deliverStore(){
                let param = {
                    "id": this.id,
                    "proves": this.confirmEnd.proves,
                    "details": [],
                    "endTime": this.utils.curDateTime(new Date())
                }
                // 添加检查逻辑
                const requiredFields = ['productid', 'grossweight', 'tareweight', 'deductionweight', 'deductionratio', 'price', 'amount'];
                let hasEmptyField = false;

                this.stockList.forEach((item)=>{
                    // 检查必填字段是否为空
                    for (const field of requiredFields) {
                        if (item[field] === null || item[field] === undefined || item[field] === '') {
                            hasEmptyField = true;
                            // this.$message.error(`第${this.stockList.indexOf(item) + 1}行的${field}不能为空`);
                            break;
                        }
                    }
                    
                    // 如果发现空字段，停止后续处理
                    if (hasEmptyField) return

                    param.details.push({
                        productid: item.productid,
                        grossweight: item.grossweight,
                        tareweight: item.tareweight || 0,
                        deductionweight: item.deductionweight || 0,
                        deductionratio: item.deductionratio || 0,
                        price: item.price,
                        amount: item.amount,
                        remarks: item.remarks
                    })
                })

                // 如果有空字段，阻止提交
                if (hasEmptyField) {
                    param.details = []; // 清空已添加的数据
                    this.$message.error(`请完整填写核实出库数据`);
                    return;
                }
                this.$confirm('您确认要结单吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    console.log('param',param)
                    this.$axios.post('/Store/OutOrder/End',param).then((data)=>{
                        this.$message.success('出库成功');
                        this.getDetails(this.id)
                    })
                }).catch(() => {

                });
            }
            // deliverStore(){ //确定出库
            //     let form = {
            //         "id": this.id,
            //         "storeId": this.form.store.id,
            //         "deliverDetails": [

            //         ]
            //     }
            //     this.form.deliverdetails.map((item)=>{
            //         item.details.map((cell)=>{
            //             form.deliverDetails.push({
            //                 id: cell.typepath,
            //                 typeRemarks: cell.typeremarks,
            //                 weight: cell.weight
            //             })
            //         })
            //     })
            //     this.$confirm('您确认要出库吗?', '提示', {
            //         confirmButtonText: '确定',
            //         cancelButtonText: '取消',
            //         type: 'warning'
            //     }).then(() => {
            //         this.$axios.post('Admin/Store/Deliver',form).then((data)=>{
            //             this.$message.success('出库成功');
            //             this.getDetails(this.id)
            //         })
            //     }).catch(() => {

            //     });
            // }
        }
    }
</script>

<style lang="scss" scoped>
    .images-list{
        display: flex;
        .item{
            padding-right: 15px;
            position: relative;
            .el-icon-error{
                position: absolute;
                top: -8px;
                right: 7px;
                font-size: 22px;
                color: var(--red);
                background: #fff;
                border-radius: 50%;
                cursor: pointer;
                &:hover {
                    color: #f00;
                }
            }
        }
    }
    .pack{
        display: flex;
        align-items: center;
        .item{
            .name{
                font-size: 18px;
            }
            .text{
                color: var(--grey);
            }
        }
        .icon{
            font-size: 30px;
            padding: 0 40px;
            transform: rotate(90deg);
            color: var(--gray);
        }
    }
    .total {
        text-align: right;
        padding-top: 10px;
        color: var(--gray);
        font-size: 14px;
        display: flex;
        align-items: center;
        justify-content: flex-end;

        .bold {
            font-weight: bold;
            font-size: 18px;
            color: var(--orange);
        }
    }

</style>
